
//
//  CircleScrollViewController.m
//  CircleScrollVIew
//
// 轮播图视图控件

#import "CircleScrollView.h"
#import "ImageBrowsController.h"
#import "UIImageView+WebCache.h"
#import <MySport-swift.h>

@interface CircleScrollView () <UIScrollViewDelegate,UIScrollViewAccessibilityDelegate>

@property (nonatomic,strong)NSArray *images;


@property (nonatomic,strong)UIScrollView *scollView;

@property (nonatomic,strong)NSTimer *time;
@property (nonatomic,assign)NSTimeInterval duration;
@property (nonatomic,assign,getter=isplaying)BOOL playing;

@property (nonatomic,strong)UIImageView *imgView0;
@property (nonatomic,strong)UIImageView *imgView;
@property (nonatomic,strong)UIImageView *imgView1;
@property (nonatomic,strong)UIPageControl *pageControl;
@property (nonatomic,strong)NSArray *urls;

@property (strong,nonatomic)UIImageView *palceHolderView;



@property (nonatomic,copy)void(^redrectBlock)(NSInteger);


@property (nonatomic,strong)ImageBrowsController *vc;


@end

@implementation CircleScrollView


//- (instancetype)initWithFrame:(CGRect)frame
//{
//    if (self = [super init]) {
////        self.images = 
//    }
//    return  self;
//}

- (instancetype)initWithFrame:(CGRect)frame URLs:(NSArray *)urls Douration:(NSTimeInterval)douration
{
    if (self = [super init]) {
        [self updateWithFrame:frame Urls:urls Douration:douration];
    }
    return self;
}

- (void)updateWithFrame:(CGRect)frame Urls:(NSArray *)urls Douration:(NSTimeInterval)douration completeHandle:(void(^)(NSInteger index))block
{
    [self updateWithFrame:frame Urls:urls Douration:douration];
    self.redrectBlock = block;
}

- (void)updateWithFrame:(CGRect)frame Urls:(NSArray *)urls Douration:(NSTimeInterval)douration{
    self.urls = urls;
    self.currentIdex = 0;
    self.playing = NO;
    self.frame = frame;
    self.duration = douration;
    [self setScollViewWithFrame:self.frame];
    if (urls.count <= 1) {
        self.scollView.scrollEnabled = NO;
    }else{
        [self playTimeWithTimInterval:self.duration repeats:YES];
    }
}



#pragma mark 私有方法
- (NSInteger)currentIdex
{
    return [self safeIndex:_currentIdex];
}

- (NSInteger)safeIndex:(NSInteger)index{
    if (index < 0) {
        index = _urls.count + index;
    }
    if (index >= _urls.count ) {
        index = index - _urls.count;
    }
    return index;
}

- (NSString *)urlOfIndex:(NSInteger)index
{
    index = [self safeIndex:index];
    return _urls[index];
}

#pragma mark 更新srollerView
- (void)refreshScrollView
{
    [self refreshWebimageViews];
    CGFloat x = _scollView.bounds.size.width;
    self.scollView.contentOffset = CGPointMake(x, 0);
    self.pageControl.currentPage = self.currentIdex;
}


- (void)refreshWebimageViews
{
    NSString *url0 = [self urlOfIndex:(self.currentIdex - 1)];
    NSString *url = [self urlOfIndex:(self.currentIdex )];
    NSString *url1 = [self urlOfIndex:(self.currentIdex + 1)];
    [_imgView0 sd_setImageWithURL:[NSURL URLWithString:url0] placeholderImage:[UIImage imageNamed:@"netease_default"]];
    [_imgView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"netease_default"]];
    [_imgView1 sd_setImageWithURL:[NSURL URLWithString:url1] placeholderImage:[UIImage imageNamed:@"netease_default"]];
}

#pragma mark scrollerView的初始化
- (void)setScollViewWithFrame:(CGRect)frame
{
    _scollView = [[UIScrollView alloc]initWithFrame:frame];
    CGFloat width = _scollView.bounds.size.width;
    CGFloat height = _scollView.bounds.size.height;
    _scollView.contentSize = CGSizeMake(width * 3, height);
    _scollView.contentOffset = CGPointMake(width, 0);
    
    _scollView.pagingEnabled = YES;
    _scollView.showsHorizontalScrollIndicator = NO;
    _scollView.showsVerticalScrollIndicator = NO;
    _scollView.maximumZoomScale = 5.0;
    _scollView.minimumZoomScale =0.1;
    
    _imgView0 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
    _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(width, 0, self.bounds.size.width, self.bounds.size.height)];
    _imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(width * 2, 0, self.bounds.size.width, self.bounds.size.height)];
    
    [self refreshWebimageViews];
    [self addGesture:_imgView];
    _imgView.userInteractionEnabled = true;
    [_scollView addSubview:_imgView0];
    [_scollView addSubview:_imgView];
    [_scollView addSubview:_imgView1];
    
    _scollView.delegate = self;
    [self addSubview:_scollView];
}

#pragma mark 设置播放时间，是否重复；默认不开启播放
- (void)playTimeWithTimInterval:(NSTimeInterval)timeInterval repeats:(BOOL)isRepeats
{
    if (self.urls.count > 1) {
        self.duration = timeInterval;
        self.playing = YES;
        [self.time invalidate];
        self.time = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(timeUpdateToScrollerView) userInfo:nil repeats:isRepeats];
        [[NSRunLoop mainRunLoop] addTimer:self.time forMode:NSRunLoopCommonModes];
    }
    
}
- (void)timeUpdateToScrollerView
{
    CGFloat x = self.bounds.size.width * 2;
    [UIView animateWithDuration:3.0 animations:^{
        [self.scollView setContentOffset:CGPointMake(x, 0.0) animated:NO];
    }];
}

#pragma mark 设置pagecontrol是否允许点击切换，默认为NO；

- (void)setPageContronEnable:(BOOL)enable
{
    self.pageControl.enabled = YES;
    
}

#pragma mark scrollerView的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat page = scrollView.contentOffset.x / self.bounds.size.width;
    if (page == 2) {
            self.currentIdex++;
            self.redrectBlock(self.currentIdex);
            [self refreshScrollView];
        }
    if (page == 0) {
            self.currentIdex--;
            self.redrectBlock(self.currentIdex);
            [self refreshScrollView];
        }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (self.isplaying == YES) {
        self.time = [NSTimer scheduledTimerWithTimeInterval:_duration target:self selector:@selector(timeUpdateToScrollerView) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:self.time forMode:NSRunLoopCommonModes];
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.time invalidate];
}


#pragma mark 给图片加载手势，加载图片浏览器

- (void)addGesture:(UIImageView *)imageView
{
    UITapGestureRecognizer *tap =  [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(junpToPhotoBrowns:)];
    [imageView addGestureRecognizer:tap];
}

- (void)junpToPhotoBrowns:(UITapGestureRecognizer *)tap
{
    [self.delegate circleScrollView:self didSelectedPage:self.currentIdex sourceView:self.imgView img:self.imgView.image];
}

@end
